//
//  GoodPhotoCell.m
//  jipin
//
//  Created by lcb on 5/21/15.
//  Copyright (c) 2015 liuchengbin. All rights reserved.
//

#import "GoodPhotoCell.h"
#import "ZShare.h"
#import "EXPhotoViewer.h"
@interface GoodPhotoCell ()
{
    
}
@property (nonatomic, strong) UIView *bgView;
@property(nonatomic,strong)UIImageView *timeIcon;
@property(nonatomic,strong)UILabel *timeLabel;
@property(nonatomic,strong)UIView *timeLine;
@property(nonatomic,strong)UIImageView *imagesView;
@property(nonatomic,strong)RTLabel *contentLabel;
@end
@implementation GoodPhotoCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor=[UIColor clearColor];
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        _bgView.backgroundColor =[UIColor clearColor];
        [self.contentView addSubview:_bgView];
        
        _imagesView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _imagesView.backgroundColor =UIColorFromRGB(0xffffff);
        _imagesView.contentMode = UIViewContentModeScaleAspectFit;
        [_bgView addSubview:_imagesView];
        
        _contentLabel =[[RTLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.textColor = [UIColor darkGrayColor];
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        _contentLabel.font = FONT(14);
        _contentLabel.lineSpacing =7;
        [self.contentView addSubview:_contentLabel];
        
        _timeIcon =[[UIImageView alloc] initWithFrame:CGRectZero];
        _timeIcon.image =[UIImage imageNamed:@"time_ico.png"];
        [_bgView addSubview:_timeIcon];
        
        _timeLabel =[[UILabel alloc] initWithFrame:CGRectZero];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.textColor = [UIColor darkGrayColor];
        _timeLabel.textAlignment = NSTextAlignmentLeft;
        _timeLabel.font = FONT(14);
        [_bgView addSubview:_timeLabel];
        
        _timeLine = [[UIView alloc] initWithFrame:CGRectZero];
        _timeLine.backgroundColor =UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_timeLine];
        
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
        self.contentView.userInteractionEnabled =YES;
        [self.contentView addGestureRecognizer:singleRecognizer];
        singleRecognizer=nil;
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.imagesView.image=nil;
    self.imagesView=nil;
    self.timeIcon=nil;
    self.timeLabel=nil;
    self.timeLine=nil;
    self.bgView=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _bgView.frame =CGRectMake(PHOTO_FRAME_WIDTH, 0, self.bounds.size.width-PHOTO_FRAME_WIDTH, self.bounds.size.height);
}

/// 根据数据模型来显示内容
- (void)showInfo:(id)model indexPath:(NSIndexPath *)indexPath
{
    self.data =model;
    self.indexPath =indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    CGFloat fTop =0;
   
    NSString *append_dateinfo =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"append_dateinfo"]] replaceNullString];
    if(append_dateinfo.length>0)
    {
        _timeLine.frame=CGRectMake(0, fTop, DEVICE_WIDTH, 1);
        _timeIcon.frame =CGRectMake(0, fTop+PHOTO_FRAME_WIDTH, widthAuto(17), hightAuto(17));
        _timeLabel.frame =CGRectMake(widthAuto(17)+PHOTO_FRAME_WIDTH, fTop+PHOTO_FRAME_WIDTH, DEVICE_WIDTH-widthAuto(17)-PHOTO_FRAME_WIDTH*2, hightAuto(17));
        _timeLabel.text = append_dateinfo;
        fTop = fTop +hightAuto(17)+PHOTO_FRAME_WIDTH*2;
    }
    NSString *ustr =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"url"]] replaceNullString];
    if (ustr.length > 0) {
        CGFloat w =[[dict objectForKey:@"width"] floatValue];
        CGFloat h =[[dict objectForKey:@"height"] floatValue];
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH - PHOTO_FRAME_WIDTH*2;
            CGFloat ratio = fullW / w ;
            CGFloat W = ratio * w;
            CGFloat H = ratio * h;
            _imagesView.frame = CGRectMake((fullW-W)/2,fTop, W, H);
        }else{
            _imagesView.frame = CGRectMake(0,fTop, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*2, DEVICE_WIDTH - PHOTO_FRAME_WIDTH*2);
        }
    
        __block UIActivityIndicatorView *activityIndicator=nil;
        __weak UIImageView *weakImageView = self.imagesView;
        [weakImageView sd_setImageWithURL:[NSURL URLWithString:ustr] placeholderImage:nil options:SDWebImageLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            if (!activityIndicator)
            {
                [weakImageView addSubview:activityIndicator = [UIActivityIndicatorView.alloc initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]];
                activityIndicator.backgroundColor=[UIColor clearColor];
                activityIndicator.center = weakImageView.center;
                [activityIndicator startAnimating];
            }
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {
                weakImageView.image=[UIImage imageNamed:@"image.png"] ;
            }else{
                weakImageView.image = image;
            }
            [weakImageView.layer addAnimation:[CATransition animation] forKey:kCATransition];
            [activityIndicator removeFromSuperview];
            activityIndicator = nil;
        }];
        fTop += _imagesView.frame.origin.y+_imagesView.frame.size.height+PHOTO_FRAME_WIDTH*2;
    }
    NSString *content =[[NSString stringWithFormat:@"%@", [dict objectForKey:@"title"]] replaceNullString];
    if (content.length>0) {
        content =[NSString stringWithFormat:@"%@",content];
        [_contentLabel setText:content];
        CGSize optimumSize1 = [_contentLabel optimumSize];
        _contentLabel.frame =CGRectMake(PHOTO_FRAME_WIDTH, fTop, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2, optimumSize1.height);
        _contentLabel.text =content;
    }
    
   [self layoutSubviews];
}

/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict =(NSDictionary*)model;
    CGFloat H =0;
    NSString *append_dateinfo =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"append_dateinfo"]] replaceNullString];
    if(append_dateinfo.length>0)
    {
        H = H +hightAuto(17)+PHOTO_FRAME_WIDTH*2;
    }
    
    NSString *ustr =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"url"]] replaceNullString];
    if (ustr.length>0) {
        CGFloat w =[[dict objectForKey:@"width"] floatValue];
        CGFloat h =[[dict objectForKey:@"height"] floatValue];
        if (w>0 && h>0) {
            CGFloat fullW =DEVICE_WIDTH - PHOTO_FRAME_WIDTH*2;
            CGFloat ratio = fullW / w;
            H = H+ratio * h;
        }else{
            H=H+DEVICE_WIDTH - PHOTO_FRAME_WIDTH*2;
        }
        H +=PHOTO_FRAME_WIDTH*2;
    }
   
    NSString *title =[[NSString stringWithFormat:@"%@",[dict objectForKey:@"title"]] replaceNullString];
    if (title.length>0) {
        title =[NSString stringWithFormat:@"%@",title];
        RTLabel *textLabel =[[RTLabel alloc] initWithFrame:CGRectMake(PHOTO_FRAME_WIDTH,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*2,0)];
        textLabel.font = FONT(14);
        textLabel.lineSpacing = 8;
        [textLabel setText:title];
        CGSize optimumSize1 = [textLabel optimumSize];
         textLabel=nil;
        CGFloat hh1 =optimumSize1.height;
        H = H+hh1+PHOTO_FRAME_WIDTH*2;
    }
    
    return H;
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)handleSingleTapFrom
{
    [EXPhotoViewer showImageFrom:_imagesView];
    if (self.actionBlock) {
        self.actionBlock(BEventType_photo,nil,self.data,nil,self.indexPath);
    }
}
-(void)reloadcell
{
   if (self.actionBlock) {
            self.actionBlock(BEventType_reload,nil,self.data,nil,self.indexPath);
   }
   
}
@end
